package com.example.myapplication1
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.PersistableBundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import java.util.*

class MainActivity : AppCompatActivity(){
    val TAG="@@MainActivity"
    private var username:String?=null
    private var password:String?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var iptUsername=findViewById<EditText>(R.id.ipt_username)
        var iptPassword=findViewById<EditText>(R.id.ipt_password)

        val usernameWatcher=object :TextWatcher
        {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"beforeTextChanged:$p0")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"onTextChanged:$p0")
            }

            override fun afterTextChanged(p0: Editable?) {
                Log.i(TAG,"afterTextChanged:$p0")
            }

        }
        iptUsername.addTextChangedListener(usernameWatcher)

        val passwordWatcher=object :TextWatcher
        {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"beforeTextChanged:$p0")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"onTextChanged:$p0")
            }

            override fun afterTextChanged(p0: Editable?) {
                Log.i(TAG,"afterTextChanged:$p0")
                password=p0?.toString()
            }

        }
        iptUsername.addTextChangedListener(usernameWatcher)
        iptPassword.addTextChangedListener(passwordWatcher)

    }
    fun onClick(view: View) {
        Log.d(TAG, "username=$username")
        Log.d(TAG, "password=$password")
        AlertDialog.Builder(this)
            .setTitle("请确定")
            .setMessage("您确定要登录吗？")
            .setNegativeButton("取消") { _, _ ->
                Toast.makeText(this, "已取消", Toast.LENGTH_LONG).show()
            }
            .setPositiveButton("确定") { _, _ ->
                toLogin()
            }
            .show()
    }
         private fun toLogin() {
            val progress = findViewById<ProgressBar>(R.id.progress)
            progress.visibility = View.VISIBLE
            progress.progress = 0
            val timer = Timer()
             val context=this
            val task = object : TimerTask() {
                override fun run() {
                    Log.d(TAG,"progress=${progress.progress}")
                    progress.progress = progress.progress + 1
                    if (progress.progress == 10) {
                        timer.cancel()
                        Handler(Looper.getMainLooper()).post{
                        progress.visibility =View.GONE

                            startActivity(Intent(context, com.example.myapplication1.ListActivity::class.java))
                        }
                    }
                }

            }
             timer.schedule(task,0,1000)
        }

}